﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace RISMOTAXDP.EmailService.MailProcess
{
    /// <summary>
    /// 邮件处理基类
    /// </summary>
    public abstract class MailProcessBase : IMailProcess
    {
        /// <summary>
        /// SQL获取指定业务类型的前置邮件
        /// </summary>
        private const string SQL_SELECT_MAIL = @"select 
Mail_ID,OperationCode,MailSubject,
MailRcpt,MailContent,MailCreateDate,MailFrom 
from vwMailForward_List where 1=1 and OperationCode=@OperationCode order by MailCreateDate";

        /// <summary>
        /// SQL保存邮件
        /// </summary>
        private const string SQL_INSERT_MAIL = @"insert TBL_MailQueueProcess
(Mail_ID,OperationCode,MailSubject,MailRcpt,MailContent,MailCreateDate,MailStatus)
values(@MailID,@OperationCode,@MailSubject,@MailRcpt,@MailContent,@MailCreateDate,0)";

        /// <summary>
        /// SQL移除前置邮件
        /// </summary>
        private const string SQL_REMOVE_FORWARDMAIL = @"delete TBL_MailForward
where Mail_ID=@MailID ";
        /*
        /// <summary>
        /// SQL更新邮件状态查询语句-生成完成
        /// </summary>
        private const string SQL_UPDATE_MAILSTATUS_SUCCESS = "UPDATE OP_MailSend SET MailContent=@MailContent,status=dbo.FuncGetFlagMailSTCreated(),AttacheFileCount=@AttacheFileCount,FileCreateDate=@FileCreateDate WHERE ID = @MailId";

        /// <summary>
        /// SQL更新邮件状态查询语句-处理中
        /// </summary>
        private const string SQL_UPDATE_MAILSTATUS_PROCESS = "UPDATE OP_MailSend SET MailContent=@MailContent,status=dbo.FuncGetFlagMailSTProcess(),AttacheFileCount=@AttacheFileCount,FileCreateDate=@FileCreateDate WHERE ID = @MailId";
        /// <summary>
        /// SQL更新邮件状态查询语句
        /// </summary>
        private const string SQL_UPDATE_MAILSTATUS_FAUILED = "UPDATE OP_MailSend SET status=dbo.FuncGetFlagMailSTCreateErr() WHERE ID = @MailId";
        /// <summary>
        /// 获取当前数据库时间
        /// </summary>
        private const string SQL_GETDATE = "SELECT GETDATE()";
        */

        /// <summary>
        /// 数据库连接
        /// </summary>
        protected SqlConnection m_Conn;

        /// <summary>
        /// 处理查询结果数据集
        /// </summary>
        protected System.Data.DataSet m_DataSet;


        /// <summary>
        /// 邮件业务数据
        /// </summary>
        protected string m_MailOPData;

        /// <summary>
        /// 邮件模板内容
        /// </summary>
        protected string m_MailTemplateContent;


        /// <summary>
        /// 获取当前处理的业务类型代码
        /// </summary>
        public abstract string GetMailOPCode();






        #region IMailProcess 成员
       
        /// <summary>
        /// 设定参数信息
        /// </summary>
        /// <param name="conn">数据库连接</param>
        public bool SetParameters(SqlConnection conn)
        {
            m_Conn = conn;
            if (m_DataSet != null)
            {
                m_DataSet.Dispose();
                m_DataSet = null;
            }
            //加载邮件模板数据
            if (m_MailTemplateContent == null || m_MailTemplateContent == string.Empty)
            {

                string MailTemplate = System.IO.Path.Combine(GlobalObject.MailTemplatePath, GlobalObject.DataBaseInfo.MailOperationList[GetMailOPCode()].MailTemplate);

                try
                {
                    using (System.IO.StreamReader streamReader = new System.IO.StreamReader(MailTemplate, Encoding.UTF8))
                    {

                        streamReader.ReadLine();
                        m_MailTemplateContent = streamReader.ReadToEnd();

                    }


                }
                catch (Exception ex)
                {
                    GlobalObject.WriteOperationLog("E81000001", ex, GlobalObject.DataBaseInfo.MailOperationList[GetMailOPCode()].OpCode);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 返回要生成的邮件记录数
        /// </summary>
        /// <returns>返回记录数</returns>
        public virtual int CheckData()
        {
            try
            {
                SqlParameter[] sParms = { new SqlParameter("@OperationCode", GetMailOPCode()) };
                m_DataSet = SQLHelper.ExecuteDataset(m_Conn, CommandType.Text, SQL_SELECT_MAIL, sParms);
            }
            catch (Exception ex)
            {
                GlobalObject.WriteOperationLog("E81000002", ex, GlobalObject.DataBaseInfo.MailOperationList[GetMailOPCode()].OpCode);
              
            }
            return m_DataSet != null ? m_DataSet.Tables[0].Rows.Count : 0;

 
        }

        /// <summary>
        /// 执行生成指定的邮件
        /// </summary>
        /// <param name="index">邮件索引</param>
        /// <returns>返回执行是否成功</returns>
        public abstract bool Process(int index);

        #endregion


        /// <summary>
        /// 保存邮件
        /// </summary>
        /// <param name="mailID"></param>
        /// <param name="operationCode"></param>
        /// <param name="mailSubject"></param>
        /// <param name="mailRcpt"></param>
        /// <param name="mailContent"></param>
        /// <param name="mailCreateDate"></param>
        protected void SaveMail(Guid mailID, string operationCode, string mailSubject, string mailRcpt, string mailContent, DateTime mailCreateDate)
        {
            //将生成内容的邮件加入TBL_MailQueueProcess表中
            SqlParameter[] sParms = { new SqlParameter("@MailID", mailID),
                                                    new SqlParameter("@OperationCode", operationCode),
                                                    new SqlParameter("@MailSubject", mailSubject),
                                                    new SqlParameter("@MailRcpt", mailRcpt),
                                                    new SqlParameter("@MailContent", mailContent),
                                                    new SqlParameter("@MailCreateDate", mailCreateDate)
                        
                                                };
            SQLHelper.ExecuteNonQuery(m_Conn, CommandType.Text, SQL_INSERT_MAIL, sParms);

        }

        /// <summary>
        /// 移除提定的前置邮件
        /// </summary>
        /// <param name="mailID"></param>
        protected void RemoveForwardMail(Guid mailID)
        {
            SqlParameter[] sParms = { new SqlParameter("@MailID", mailID) };


            SQLHelper.ExecuteNonQuery(m_Conn, CommandType.Text, SQL_REMOVE_FORWARDMAIL, sParms);
        }


        #region IDisposable 成员

        public void Dispose()
        {
            if (m_DataSet != null)
            {
                m_DataSet.Dispose();
                m_DataSet = null;
            }
        }

        #endregion


    }
}
